package hk.m4s.lr.repair.test.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import framework.common.Constant;
import framework.common.zanetwork.UeHttpUrl;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.MyOrderModel;
import hk.m4s.lr.repair.test.model.UserinfoModel;
import hk.m4s.lr.repair.test.service.user.MyService;
import hk.m4s.lr.repair.test.ui.baoyang.MyBaoYangListActivity;
import hk.m4s.lr.repair.test.ui.equipment.MyEquipmentActivity;
import hk.m4s.lr.repair.test.ui.user.AppSetActivity;
import hk.m4s.lr.repair.test.ui.user.CollectingGoodsActivity;
import hk.m4s.lr.repair.test.ui.user.MyCareGoodsActivity;
import hk.m4s.lr.repair.test.ui.user.MyCouponActivity;
import hk.m4s.lr.repair.test.ui.user.MyFootGoodsActivity;
import hk.m4s.lr.repair.test.ui.user.MyToolkitActivity;
import hk.m4s.lr.repair.test.ui.user.MyTuioderActivity;
import hk.m4s.lr.repair.test.ui.user.MyWaiteMoneyActivity;
import hk.m4s.lr.repair.test.ui.user.MyoderActivity;
import hk.m4s.lr.repair.test.ui.user.MyoderEnValueActivity;
import hk.m4s.lr.repair.test.ui.user.NoticeActivity;
import hk.m4s.lr.repair.test.ui.user.ShopAddressActivity;
import hk.m4s.lr.repair.test.ui.user.TakeToolkitActivity;
import hk.m4s.lr.repair.test.ui.user.ToolkitActivity;
import hk.m4s.lr.repair.test.ui.user.UnprocessedMoneyActivity;
import hk.m4s.lr.repair.test.ui.user.UserEditActivity;
import hk.m4s.lr.repair.test.ui.webview.WebViewActivity;
import hk.m4s.lr.repair.test.utils.SharedPreferencesUtil;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import hk.m4s.lr.repair.test.views.CornersTransform;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/13.
 *
 * /**
 1.总经理
 2.设备管理员
 3.班长
 4.其他
 5.车间主管
 6.配件主管
 7.售后总监
 8.售后经理
 */

public class MyFragment extends Fragment implements View.OnClickListener {
    private Context context;
    private LayoutInflater inflater;
    private RelativeLayout next_user_edit, click_baoyang, click_shebei, click_set, click_about,click_toolkit,click_special_tools,clickCoupon,click_address;
    private TextView user_name,user_admin,user_company,baoyang_number,user_wait_money,main_waite_shouhuo,user_wait_goods,main_waite_envalue,main_waite_tuihuan;
    private ImageView user_head,messages,red_hot;
    private TextView main_all_order;
    private RelativeLayout click_care,click_foot;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_fragment_layout, container, false);
    }
    private UserinfoModel userinfoModels;
    private String uerId="";
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        // TODO Auto-generated method stub
        context = getActivity();
        inflater = LayoutInflater.from(context);
        findBaseView();
    }

    private void findBaseView() {
        user_name=(TextView)this.getActivity().findViewById(R.id.user_name);
        user_admin=(TextView)this.getActivity().findViewById(R.id.user_admin);
        user_company=(TextView)this.getActivity().findViewById(R.id.user_company);
        baoyang_number=(TextView)this.getActivity().findViewById(R.id.baoyang_number);
        user_wait_money=(TextView)this.getActivity().findViewById(R.id.user_wait_money);
        user_wait_goods=(TextView)this.getActivity().findViewById(R.id.user_wait_goods);
        main_waite_shouhuo=(TextView)this.getActivity().findViewById(R.id.main_waite_shouhuo);
        main_waite_envalue=(TextView)this.getActivity().findViewById(R.id.main_waite_envalue);
        main_waite_tuihuan=(TextView)this.getActivity().findViewById(R.id.main_waite_tuihuan);
        main_all_order=(TextView)this.getActivity().findViewById(R.id.main_all_order);
        user_head=(ImageView) this.getActivity().findViewById(R.id.user_head);
        messages=(ImageView)this.getActivity().findViewById(R.id.messages);
        red_hot=(ImageView)this.getActivity().findViewById(R.id.red_dot);
        click_toolkit= (RelativeLayout) this.getActivity().findViewById(R.id.click_toolkit);
        next_user_edit = (RelativeLayout) this.getActivity().findViewById(R.id.next_user_edit);
        click_baoyang = (RelativeLayout) this.getActivity().findViewById(R.id.click_baoyang);
        click_shebei = (RelativeLayout) this.getActivity().findViewById(R.id.click_shebei);
        click_set = (RelativeLayout) this.getActivity().findViewById(R.id.click_set);
        click_about = (RelativeLayout) this.getActivity().findViewById(R.id.click_about);
        click_special_tools= (RelativeLayout) this.getActivity().findViewById(R.id.click_special_tools);
        clickCoupon= (RelativeLayout) this.getActivity().findViewById(R.id.click_coupon);
        click_address= (RelativeLayout) this.getActivity().findViewById(R.id.click_address);
        click_care= (RelativeLayout) this.getActivity().findViewById(R.id.click_care);
        click_foot= (RelativeLayout) this.getActivity().findViewById(R.id.click_foot);
        click_foot.setOnClickListener(this);
        click_about.setOnClickListener(this);
        click_set.setOnClickListener(this);
        next_user_edit.setOnClickListener(this);
        click_baoyang.setOnClickListener(this);
        click_shebei.setOnClickListener(this);
        messages.setOnClickListener(this);
        click_toolkit.setOnClickListener(this);
        click_special_tools.setOnClickListener(this);
        clickCoupon.setOnClickListener(this);
        click_address.setOnClickListener(this);
        click_care.setOnClickListener(this);
        user_wait_money.setOnClickListener(this);
        main_waite_shouhuo.setOnClickListener(this);
        user_wait_goods.setOnClickListener(this);
        main_waite_envalue.setOnClickListener(this);
        main_waite_tuihuan.setOnClickListener(this);
        main_all_order.setOnClickListener(this);
        if(SharedPreferencesUtils.getSharedPreferences(Constant.ifadmin,"").equals("2")) {
            click_special_tools.setVisibility(View.GONE);
            click_toolkit.setVisibility(View.VISIBLE);
            click_shebei.setVisibility(View.VISIBLE);
            click_baoyang.setVisibility(View.VISIBLE);
        }else if(SharedPreferencesUtils.getSharedPreferences(Constant.ifadmin,"").equals("3")){
            click_special_tools.setVisibility(View.VISIBLE);
            click_toolkit.setVisibility(View.VISIBLE);
            click_shebei.setVisibility(View.VISIBLE);
            click_baoyang.setVisibility(View.VISIBLE);
        }else{
            click_special_tools.setVisibility(View.GONE);
            click_toolkit.setVisibility(View.GONE);
            click_shebei.setVisibility(View.GONE);
            click_baoyang.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_user_edit:

                    Intent userIntent = new Intent(getActivity(), UserEditActivity.class);
                    userIntent.putExtra("userinfo", userinfoModels);
                    getActivity().startActivity(userIntent);

                break;
            case R.id.click_baoyang:
                if(SharedPreferencesUtil.getInstance().getString("members_id")==null||SharedPreferencesUtil.getInstance().getString("members_id").equals("")){
                    ToastUtil.toast(context,"非企业用户不能使用");
                }else {
                    Intent byIntent = new Intent(getActivity(), MyBaoYangListActivity.class);
                    startActivity(byIntent);
                }
                break;
            case R.id.click_shebei:
                if(SharedPreferencesUtil.getInstance().getString("members_id")==null||SharedPreferencesUtil.getInstance().getString("members_id").equals("")){
                    ToastUtil.toast(context,"非企业用户不能使用");
                }else {
                    Intent sbIntent = new Intent(getActivity(), MyEquipmentActivity.class);
                    startActivity(sbIntent);
                }
                break;
            case R.id.click_set:
                Intent setIntent = new Intent(getActivity(), AppSetActivity.class);
                startActivity(setIntent);
                break;
            case R.id.click_about:
                Intent aboutIntent = new Intent(getActivity(), WebViewActivity.class);
                aboutIntent.putExtra("title", "关于我们");
                aboutIntent.putExtra("url",UeHttpUrl.about);
                aboutIntent.putExtra("show", "0");
                startActivity(aboutIntent);
                break;
            case R.id.click_toolkit:
                if(SharedPreferencesUtil.getInstance().getString("members_id")==null||SharedPreferencesUtil.getInstance().getString("members_id").equals("")){
                    ToastUtil.toast(context,"非企业用户不能使用");
                }else {
                    Intent ktiIntent = null;
                    if (SharedPreferencesUtils.getSharedPreferences(Constant.ifadmin, "").equals("2")) {
                        ktiIntent = new Intent(getActivity(), MyToolkitActivity.class);
                    } else {
                        ktiIntent = new Intent(getActivity(), ToolkitActivity.class);
                    }
                    ktiIntent.putExtra("type", "1");
                    ktiIntent.putExtra("title", "工具箱");
                    startActivity(ktiIntent);
                }
                break;
            case R.id.click_special_tools:
                if(SharedPreferencesUtil.getInstance().getString("members_id")==null||SharedPreferencesUtil.getInstance().getString("members_id").equals("")){
                    ToastUtil.toast(context,"非企业用户不能使用");
                }else {
                    Intent ktisIntent = new Intent(getActivity(), TakeToolkitActivity.class);
                    ktisIntent.putExtra("type", "2");
                    ktisIntent.putExtra("title", "借用工具");
                    startActivity(ktisIntent);
                }
                break;
            case R.id.click_coupon:
                Intent couponin=new Intent(getActivity(), MyCouponActivity.class);
                startActivity(couponin);
                break;
            case R.id.click_address:
                Intent addressin=new Intent(getActivity(), ShopAddressActivity.class);
                startActivity(addressin);
                break;
            case R.id.click_care:
                Intent carein=new Intent(getActivity(), MyCareGoodsActivity.class);
                startActivity(carein);
                break;
            case R.id.click_foot:
                Intent footin=new Intent(getActivity(), MyFootGoodsActivity.class);
                startActivity(footin);
                break;
            case R.id.messages:
                Intent in=new Intent(getActivity(), NoticeActivity.class);
                if(userinfoModels!=null) {
                    in.putExtra("sysNotice", userinfoModels.getSysNotice());
                    in.putExtra("businessNotice", userinfoModels.getBusinessNotice());
                }
                startActivity(in);
                break;
            case R.id.user_wait_money:
                  Intent orderIntent=new Intent(getActivity(), MyWaiteMoneyActivity.class);
                  orderIntent.putExtra("tabSelect","1");
                  startActivity(orderIntent);
                break;
            case R.id.user_wait_goods:
                Intent fahuoIntents = new Intent(getActivity(), UnprocessedMoneyActivity.class);
                fahuoIntents.putExtra("title", "待发货");
                fahuoIntents.putExtra("state", "2");
                getActivity().startActivity(fahuoIntents);
                break;
            case R.id.main_waite_shouhuo:
                Intent shouhuoIntents = new Intent(getActivity(), CollectingGoodsActivity.class);
                shouhuoIntents.putExtra("title", "待收货");
                shouhuoIntents.putExtra("state", "3");
                getActivity().startActivity(shouhuoIntents);
                break;
            case R.id.main_waite_envalue:
                Intent orderenvalueIntent=new Intent(getActivity(), MyoderEnValueActivity.class);
                startActivity(orderenvalueIntent);
                break;
            case R.id.main_waite_tuihuan:
                Intent orderTuioderIntent=new Intent(getActivity(), MyTuioderActivity.class);
                startActivity(orderTuioderIntent);
                break;
            case R.id.main_all_order:
                Intent orderMyoderIntent=new Intent(getActivity(), MyoderActivity.class);
                startActivity(orderMyoderIntent);
                break;
        }
    }

    public void getUserInfo() {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
        JSONObject jsondata = new JSONObject();
        parmas.put("jsonText", jsondata.toString());
        MyService.userInfo(context, parmas, new ResponseCallback<UserinfoModel>() {
            @Override
            public void onSuccess(UserinfoModel userinfoModel) {
                userinfoModels=userinfoModel;
                if(!(userinfoModel.getUserName()==null||userinfoModel.getUserName().equals(""))) {
                    user_name.setText(userinfoModel.getUserName());
                }else{
                    user_name.setText("会员ID "+userinfoModel.getUserID());
                }
                if(!userinfoModel.getPosition().equals("")) {
                    user_admin.setText("(" + userinfoModel.getPosition() + ")");
                }
                if(!(userinfoModel.getCompanyName()==null||userinfoModel.getUserName().equals(""))) {
                    user_company.setText(userinfoModel.getCompanyName());
                }else{
                    user_company.setText("未设置公司名称");
                }

                uerId=userinfoModel.getUserID()+"";
                if(Integer.parseInt(userinfoModel.getTaskNum())>0) {
                    baoyang_number.setText(userinfoModel.getTaskNum());
                }
                if(SharedPreferencesUtils.getSharedPreferences(Constant.ifadmin,"").equals("2")) {
                    red_hot.setVisibility(View.VISIBLE);
                    if(Integer.parseInt(userinfoModel.getSysNotice())>0){
                        red_hot.setVisibility(View.VISIBLE);
                    }else if(Integer.parseInt(userinfoModel.getBusinessNotice())>0){
                        red_hot.setVisibility(View.VISIBLE);
                    }else{
                        red_hot.setVisibility(View.GONE);
                    }
                }else{
                    red_hot.setVisibility(View.GONE);
                }
                SharedPreferencesUtils.addgetSharedPreferences(Constant.address,String.valueOf(userinfoModel.getLogo()));
                SharedPreferencesUtils.addgetSharedPreferences(Constant.address,String.valueOf(userinfoModel.getAddressCount()));
                try {
                    Glide.with(context).load(userinfoModel.getLogo()).asBitmap().centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL)
                            .transform(new CornersTransform(context))
                            .placeholder(R.mipmap.default_head)
                            .error(R.mipmap.default_head)
                            .into(new BitmapImageViewTarget(user_head) {
                                @Override
                                protected void setResource(Bitmap resource) {
                                    RoundedBitmapDrawable circularBitmapDrawable =
                                            RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                                    circularBitmapDrawable.setCircular(true);
                                    user_head.setImageDrawable(circularBitmapDrawable);
                                }
                            });
                    SharedPreferencesUtils.addgetSharedPreferences(Constant.face,userinfoModel.getLogo());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }
}
